{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55ded70a",
   "metadata": {},
   "source": [
    "## torch.nn.Embedding\n",
    "\n",
    "- 输入：每个元素是整形索引，例如词汇表中单词索引\n",
    "- 输出：每个元素的嵌入向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "35635da9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.7105, -0.6303, -1.2601],\n",
      "        [-1.6672, -0.5169,  0.1115],\n",
      "        [-0.6503,  1.9078,  0.7153],\n",
      "        [ 0.1526,  1.1069, -1.3253]], grad_fn=<EmbeddingBackward0>)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "# num_embeddings: 词汇表大小\n",
    "# embedding_dim: 嵌入向量的维度\n",
    "embedding = nn.Embedding(num_embeddings=10, embedding_dim=3)\n",
    "\n",
    "# 输入一个包含4个单词索引的张量\n",
    "indices = torch.tensor([1, 2, 3, 4])\n",
    "\n",
    "# Get the embeddings for the indices\n",
    "embeddings = embedding(indices)\n",
    "print(embeddings)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
